package com.atwisdom.star.core.dao.pojo.basicClass;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.core.dao.pojo.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 
 * @TableName timer
 */
@TableName(value ="timer",autoResultMap = true)
@Data
public class Timer extends BaseEntity {
    /**
     * 任务标识
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 任务名称
     */
    @TableField(value = "name")
    private String name;

    /**
     * 事件名称
     */
    @TableField(value = "eventName")
    private String eventName;

    /**
     * 事件类型
     */
    @TableField(value = "eventCategory")
    private String eventCategory;

    /**
     * 事件参数
     */
    @TableField(value = "eventParam",typeHandler = FastjsonTypeHandler.class)
    private JSONObject eventParam;
    /**
     * 时间间隔，单位秒
     */
    @TableField(value = "interval")
    private Integer interval;

    /**
     * 说明
     */
    @TableField(value = "comments")
    private String comments;

    /**
     * 任务状态
     */
    @TableField(value = "status")
    private String status;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;


    public Timer(){

    }

    public static String[] fetchSimpleColumns() {
        List<String> columns = new ArrayList<>();
        columns.add("id");
        columns.add("name");
        columns.add("eventName");
        columns.add("eventCategory");
        columns.add("eventParam");
        columns.add("interval");
        columns.add("comments");
        columns.add("status");
        columns.add("updatedBy");
        columns.add("updateTime");
        columns.add("createdBy");
        columns.add("createTime");
        return columns.toArray(new String[0]);
    }

    /**
     * 有参构造方法
     *
     * @param jsonObject 参数
     */
    public Timer(JSONObject jsonObject) {
        Set<String> keys = jsonObject.keySet();
        this.id = keys.contains("id") ? jsonObject.getLong("id") : null;
        this.name = jsonObject.getString("name");
        this.eventName = keys.contains("eventName") ? jsonObject.getString("eventName") : null;
        this.eventCategory = keys.contains("eventCategory") ? jsonObject.getString("eventCategory"):null;
        this.eventParam = keys.contains("eventParam") ? jsonObject.getJSONObject("eventParam") : null;
        this.interval = keys.contains("interval") ? jsonObject.getInteger("interval") : null;
        this.comments = keys.contains("comments") ? jsonObject.getString("comments") : null;
        this.status = keys.contains("status") ? jsonObject.getString("status") : null;
    }

    /**
     * 将当前模型对象转成jsonObject
     *
     * @return 返回结果
     */
    public JSONObject fetchJsonObject() {
        JSONObject JsonObject = new JSONObject();
        JsonObject.put("id", this.id);
        JsonObject.put("name", this.name);
        JsonObject.put("eventName", this.eventName);
        JsonObject.put("eventCategory", this.eventCategory);
        JsonObject.put("eventParam", this.eventParam);
        JsonObject.put("interval", this.interval);
        JsonObject.put("status", this.status);
        JsonObject.put("comments", this.comments);
        JsonObject.put("createdBy", this.createdBy);
        JsonObject.put("createTime", this.createTime);
        JsonObject.put("updatedBy", this.updatedBy);
        JsonObject.put("updateTime", this.updateTime);
        return JsonObject;
    }
}